package org.crystal.tradecraft.serialization;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import org.crystal.tradecraft.model.binance.market.KLine;

import java.io.IOException;
import java.time.Instant;

public class BinanceKLineDeserializer extends JsonDeserializer<KLine> {

    @Override
    public KLine deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException {
        JsonNode jsonNode = jsonParser.readValueAsTree();

        return KLine.builder()
            .openTime(Instant.ofEpochMilli(jsonNode.get(0).asLong()))
            .openPrice(jsonNode.get(1).asDouble())
            .highPrice(jsonNode.get(2).asDouble())
            .lowPrice(jsonNode.get(3).asDouble())
            .closePrice(jsonNode.get(4).asDouble())
            .volume(jsonNode.get(5).asDouble())
            .closeTime(Instant.ofEpochMilli(jsonNode.get(6).asLong()))
            .amount(jsonNode.get(7).asDouble())
            .numberOfTrades(jsonNode.get(8).asInt())
            .takerBuyVolume(jsonNode.get(9).asDouble())
            .takerBuyAmount(jsonNode.get(10).asDouble())
            .build();
    }
}
